package com.gray.blog.service.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gray.blog.common.enums.DelFlagTypeEnum;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.result.ModelResult;
import com.gray.blog.common.util.PagerUtil;
import com.gray.blog.common.util.StringUtil;
import com.gray.blog.dao.sys.SysRoleDao;
import com.gray.blog.entity.sys.SysRoleEntity;
import com.gray.blog.result.sys.GetPageRoleResult;
import com.gray.blog.result.sys.RoleDetailResult;
import com.gray.blog.service.BaseService;
import com.gray.blog.vo.sys.AddSysRoleVO;
import com.gray.blog.vo.sys.GetPageRoleArgVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created  By zhoux 2019/03/11
 **/
@Service
@Slf4j
public class SysRoleService extends BaseService<SysRoleDao, SysRoleEntity> {

    @Autowired
    private SysRoleDao sysRoleDao;

    public ModelResult addSysRole(AddSysRoleVO vo) {
        SysRoleEntity sysRoleEntity = new SysRoleEntity();
        sysRoleEntity.setId(StringUtil.getUUID());
        sysRoleEntity.setName(vo.getName());
        sysRoleEntity.setEnName(vo.getEnName());
        sysRoleEntity.setRoleType(vo.getRoleType());
        sysRoleEntity.setIsSys(vo.getIsSys());
        sysRoleEntity.setRoleOrder(vo.getRoleOrder());
        sysRoleEntity.setCreateBy(vo.getUserId());
        sysRoleEntity.setUpdateBy(vo.getUserId());
        sysRoleEntity.setRemarks(vo.getRemarks());
        sysRoleEntity.setDelFlag(DelFlagTypeEnum.NOT_DELETE.getType());
        super.insert(sysRoleEntity);
        return ModelResult.newSuccess();
    }

    public ModelResult<GetPageRoleResult> getPageRole(GetPageRoleArgVO vo) {
        IPage<SysRoleEntity> pager = super.findItemByPage(vo.getPageNum(), vo.getPageSize());
        PagerUtil<GetPageRoleResult> pagerUtil = new PagerUtil<>();
        GetPageRoleResult result = pagerUtil.setPageResultByPageObject(pager, new GetPageRoleResult(), RoleDetailResult.class);
        return ModelResult.newSuccess(result);
    }

    public ModelResult deleteSysRole(String id) {
        if (this.deleteById(id)) {
            return new ModelResult<>(ErrorCodeEnum.SUCCESS);
        } else {
            log.error("SysRoleService.deleteSysRole error id:{}", id);
            return new ModelResult<>(ErrorCodeEnum.SYSTEM_ERROR);
        }
    }

}
